<?php
class Controller_Admin_Promotion extends  Controller_Template {
	public function __construct($req, $res) {
		$this->template = "smarty:admin/promotion/all_activities";
		parent::__construct($req, $res);
	}

    public function action_index ()
    {
		print_r('hello world!!');
	}

	/**
	 *
	 * 获得所有的推广活动
	 *
	 */

	public function action_all_activities($status = NULL)
	{

        $status     = empty($status) ? NULL : trim($status);
		$activities = Promotion_Activity::get_activities($status, User::current_user_id());
		$this->template->set('activities',$activities);

	}

	/**
	 *
	 * 获得渠道推广活动
	 *
	 */

	public function action_activities()
	{
		$this->template->set_filename('smarty:admin/promotion/activities');

		//可申请的活动
		$activities = Promotion_Activity::get_activities('verified', NULL, $is_leaf = 0);
		$this->template->set('activities', $activities);
	}

	/**
	 *
	 *下载apk
	 *
	 *
	 */

	public function action_downapk()
	{
		$mer_id = User::current_user_id();
		$this->template->set('globa_site_type','merchant');
		$p_vendor =  new Promotion_Vendor($mer_id);
		//已经申请的活动
		$v_activities = $p_vendor->activities();
		$rs = array();
		foreach($v_activities as $key => $val)
		{
			$activity = Promotion_activity::get_activity_by_id($val['activity_id']);
			$channel = Promotion_Channel::info($val['channel_id']);
			$rs[$key]['activity_name'] = $activity['name'];
			$rs[$key]['channel_name'] = $channel['name'];
			$rs[$key]['channel_id'] = $val['channel_id'];
			$rs[$key]['url'] = $val['url'];
			$rs[$key]['key'] = $channel['unique_key'];
		}
		$this->template->set_filename('smarty:promotion/downapk');
		$this->template->set('activities',$rs);
	}
	/**
	 *
	 * 进入到新建活动页面
	 *
	 */
	public function action_new_activity()
	{
		$this->template->set_filename('smarty:admin/promotion/new_activity');
	}

	/**
	 *
	 * 新建一个推广活动
	 *
	 */

    public function action_create_activity()
    {
		$post = $this->request->post();
        $project = Arr::get($post, 'name');
        $price   = Arr::get($post, 'price');
        $pkgname = Arr::get($post,'pkgname');
        $pay_type = Arr::get($post, 'pay_type'); 
        $pay_by   = Arr::get($post,'pay_by');
		//项目主管建立的活动默认是100，后期可以由推广主管修改。
        $ratio    = 1;
		//$activity_id 暂时没用到
        $activity_id = Promotion_Activity::create_app_project($project, $price, $pkgname, $pay_type, $pay_by, $ratio);
		$this->request->redirect('http://admin.union.appchina.com/promotion/all_activities');
	
    }

	public function action_activity_detail($ac_id)
	{
		$mer_id = User::current_user_id();
		$activity = Promotion_Activity::get_activity_by_id($ac_id);
		$channels = Promotion_Channel::channels($mer_id);
		$ac_channels = array();
		foreach($channels as $key => $val)
		{
			$ac_channels[$key]['name'] = $val['name'];
			$ac_channels[$key]['id'] = $val['id'];
				
		}
		$role_id = user::current_user()->get_role_id();
		if ($role_id == 'commonmanager')
		{
			$isCommonmanager = TRUE;
		}
		$this->template->set_filename('smarty:promotion/activity_detail');
		$this->template->set('act',$activity);
		$this->template->set('isCommonmanager',$isCommonmanager);
		$this->template->set('ac_channels',$ac_channels);
	}


	/**
	 *
	 * 获得待审核的推广申请
	 *
	 */
	public function action_unverified_applies()
	{
		$this->template->set_filename('smarty:admin/promotion/unverified_applies');
		//获得未通过审核的申请
		$list = array();
		$rs = Promotion::list_activity_channel('unverified');
		foreach($rs as $key => $val)
		{
			try{
				$activity = Promotion_activity::get_activity_by_id($val['activity_id'],User::current_user_id());
			}catch(Exception_Empty $err)
			{
				continue;
			}
			$channel = Promotion_Channel::info($val['channel_id']);
			$list[$key]['act_name'] = $activity['name'];
			$list[$key]['cha_name'] = $channel['name'];
			$list[$key]['pay_type'] = $activity['pay_type'];
			$list[$key]['mer_id'] = $val['merchant_id'];
			$list[$key]['act_id'] = $val['activity_id'];
			$list[$key]['channel_id'] = $val['channel_id'];
		}
		$this->template->set('list',$list);
		
	}

    public function action_activity_verify($activity_id)
    {
		$activity_id = (int) $activity_id;
        $activity    = new Promotion_Activity($activity_id);
        $result      = $activity->verify();
		$this->request->redirect('promotion/activities');
    }

    public function action_applies_verify ()
	{	
		$post = $this->request->query();
		$activity_id = Arr::get($post,'act_id');
		$channel_id = Arr::get($post,'channel_id');
        $target_url = Arr::get($post,'url'); 
        $promotion = new Promotion($activity_id, $channel_id);
        $promotion->verify($target_url);

        //
        // packing 
        // 
        $promotion->start_packing();
		print_r('success');exit;
    }

}
